package com.cube.truthseer.controller;


import com.cube.truthseer.common.entity.enums.ResultCode;
import com.cube.truthseer.common.entity.vo.ResultMessage;
import com.cube.truthseer.common.security.context.UserContext;
import com.cube.truthseer.common.security.token.Token;
import com.cube.truthseer.common.utils.ResultUtil;
import com.cube.truthseer.entity.user.dto.LoginFormDTO;

import com.cube.truthseer.entity.user.dto.UserFormDto;
import com.cube.truthseer.entity.user.po.User;
import com.cube.truthseer.service.ICodeService;
import com.cube.truthseer.service.IUserService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;



@Slf4j
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    @Resource
    private UserContext userContext;

    private final IUserService userService;
    private final ICodeService codeService;
    @ApiOperation("登录")
    @PostMapping("/login")
    public ResultMessage<Token> queryUser(@Valid @RequestBody LoginFormDTO loginFormDTO){
        return userService.queryUser(loginFormDTO);
    }

    @ApiOperation("获取当前登录用户消息")
    @GetMapping
    public ResultMessage<User> getUserInfo() {
        Long id = userContext.getLoginUser().getId();
        return userService.getUserInfo(id);
    }

    @ApiOperation("发送邮件给用户登录")
    @PostMapping("/sendEmailByLogin")
    public ResultMessage<String>sendEmailByLogin(String email){
         codeService.sendVerifyCode(email,1);
         return ResultUtil.success(ResultCode.VERIFICATION_SEND_SUCCESS);
    }

    @ApiOperation("发送邮件给用户注册")
    @PostMapping("/sendEmailByRegister")
    public ResultMessage<String>sendEmailByRegister(String email){
        codeService.sendVerifyCode(email,2);
        return ResultUtil.success(ResultCode.VERIFICATION_SEND_SUCCESS);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public ResultMessage<String> registerUser(@Valid @RequestBody UserFormDto userFormDto){
        return userService.saveUser(userFormDto);
    }




}
